<?php
class FunctionGameLoto
{
	public static $static = null;
	static function createArrayRandomNumber($nCount, $iMin, $iMax){
		$b = array();
		$a = array();
		$nDistance = $iMax - $iMin + 1;
		if ($nDistance < $nCount) 
			return false;
		for ($i = $iMin; $i <= $iMax; ++$i)
		{
			$b[$i-$iMin] = $i;
		}
		for ($i = 0; $i < $nCount; ++$i)
		{
			$rnd = (rand() /0x7fff) * $nDistance;
			$a[$i] = $b[$rnd]; 
			$b[$rnd] = $b[$nDistance - 1];
			--$nDistance;
		}
		return $a;
	}
        static function registerCard($zoneID,$mailaddress, $card)
        {
            $key = "zoneID_".$zoneID."_mail_".$mailaddress."_card";
            {if(!isset( FunctionData::$static  ) ||  FunctionData::$static  ==null){ FunctionData::$static   = new Memcache; FunctionData::$static  ->connect("127.0.0.1" , 11215 ) or die ("Could not connect");};} ;
            {if( FunctionData::$static  ->get( $key )==null) FunctionData::$static  ->set( $key , $card->getBinary() );else  FunctionData::$static  ->replace( $key , $card->getBinary() );} ;
        }
        static function readCardInfo($zoneID,$mailaddress)
        {
            $key = "zoneID_".$zoneID."_mail_".$mailaddress."_card";
            $reg_data =  FunctionData::$static  ->get( $key ) ;
		if($reg_data!=null){
			return Card::swap(ByteBuffer::wrap( $reg_data ) );
		}
            return null;
        }
        static function registerAccount($mailaddress, $password, $playername)
        {
            $rs = new Result();
            if(strlen($mailaddress)  > 0) 
            {
                if (preg_match("/[\w\d_]+@[.\w\d_]+/i", $mailaddress)) {
                    if (!FunctionData::checkEmailUsed($mailaddress)) 
                    {
                        $player = new DataPlayer();
                        $player->PlayerNameBytes = unpack('C*', $playername);
                        $player->MailAddressBytes = unpack('C*', $mailaddress);
                        $player->PasswordHashBytes = unpack('C*', UtilHash::sha256((strlen($password) > 0) ? $password : "" ));
                        $player->SecretKeyBytes = unpack('C*', UtilHash::generateKey(32));
                        $confirm = DataWaitingConfirm::generate();
                        {if(!isset( FunctionData::$static  ) ||  FunctionData::$static  ==null){ FunctionData::$static   = new Memcache; FunctionData::$static  ->connect("127.0.0.1" , 11215 ) or die ("Could not connect");};} ;
                        {if( FunctionData::$static  ->get( "mail_reginfo_" .$mailaddress )==null) FunctionData::$static  ->set( "mail_reginfo_" .$mailaddress , $player->getBinary() );else  FunctionData::$static  ->replace( "mail_reginfo_" .$mailaddress , $player->getBinary() );} ;
                        {if( FunctionData::$static  ->get( "mail_confirm_" .$mailaddress )==null) FunctionData::$static  ->set( "mail_confirm_" .$mailaddress , $confirm->getBinary() );else  FunctionData::$static  ->replace( "mail_confirm_" .$mailaddress , $confirm->getBinary() );} ;
                        {if( FunctionData::$static  ->get( "mng_numof_user_registed"  )!=null) FunctionData::$static  ->increment( "mng_numof_user_registed"  );else  FunctionData::$static  ->set( "mng_numof_user_registed"  ,1);} ;
                        { FunctionData::$static  ->close(); FunctionData::$static   = null;} ;
                        $rs->addData("code",$confirm->getCode());
                        $mail = new PHPMailer();
							$mail->IsSMTP();
							$mail->SMTPAuth   = true;                  	 
							$mail->SMTPSecure = "ssl";                 	 
							$mail->Host       = "smtp.gmail.com";      	 
							$mail->Port       = 465;                   	 
							$mail->Username   = "admin@colorbox.com.vn" ;  	 
							$mail->Password   = "" ;             
							$mail->From       = "admin@colorbox.com.vn" ;
							$mail->FromName   = "Colorbox Social Network";
							$mail->Subject    = "Register Confirm";
							$mail->AltBody    = "This is the body when user views in plain text format";  
							$mail->WordWrap   = 50;  
							$ptn = readPattern($_SESSION[ "service_dir" ]  .'/pattern/' .'ptn_'. "registration_letter" .'.php' );
							$mail->MsgHTML( $ptn ->publish() );
							$mail->IsHTML(true);  
							if(!$mail->Send()){
								$rs->addError(4 , "Can not send confirm mail, please contact administrator, or request another confirm code.");
							}else{
								$rs->addData("messate","Register Successful, please check mail for confirm code. Your account must be confirm before using to login.");
							}
                        $rs->return = true;
                    }
                    else
                    {
                        $rs->addError(3 ,$mailaddress." had been used for registed.");
                    }
                }
                else
                {
                    $rs->addError(1 , $mailaddress." not allow.");
                }
            }else
            {
                $rs->addError(1 , "Email address empty!");
            }
            return $rs;
        }
        static function confirmRegistration($mailaddress, $confirmcode) 
        {
            $rs = new Result();
            if (strlen($mailaddress) > 0) 
            {
                {if(!isset( FunctionData::$static  ) ||  FunctionData::$static  ==null){ FunctionData::$static   = new Memcache; FunctionData::$static  ->connect("127.0.0.1" , 11215 ) or die ("Could not connect");};} ;
                $confirm =  FunctionData::$static  ->get( "mail_confirm_" .$mailaddress ) ;
                if($confirm != null)
                {
                    $confirmobj = DataWaitingConfirm::swap(ByteBuffer::wrap( $confirm ) );
                    if ($confirmobj->getCode() == $confirmcode) {
                        $rs->return = true;
                        $id = FunctionData::registerAnUserId();
                        FunctionData::connectIdAndMailAddress($id, $mailaddress);
                         FunctionData::$static  ->delete(  "mail_confirm_"  . $mailaddress ) ;
                        {if( FunctionData::$static  ->get(  "mng_numof_user_confirm"  )!=null) FunctionData::$static  ->increment(  "mng_numof_user_confirm"  );else  FunctionData::$static  ->set(  "mng_numof_user_confirm"  ,1);} ;
                    } else {
                        $rs->return = false;
                        $rs->addError(6 , "Wrong code!");
                    }
                } else {
                    $rs->addError(5 , "Confirm error, please contact to Administrator");
                }
            }
            else
            {
                $rs->addError(1 ,"Email address empty!");
            }
            return $rs;
        }
        static function readAccountRegisterInfo($email_address){
		{if(!isset( FunctionData::$static  ) ||  FunctionData::$static  ==null){ FunctionData::$static   = new Memcache; FunctionData::$static  ->connect("127.0.0.1" , 11215 ) or die ("Could not connect");};} ;
		$reg_data =  FunctionData::$static  ->get( "mail_reginfo_" .$email_address ) ;
		if($reg_data!=null){
			return DataPlayer::swap(ByteBuffer::wrap( $reg_data ) );
		}
		return null;
	}
}   
?>